package com.therandoms.rtgdroid.background;

import java.io.FileInputStream;

import javax.xml.parsers.DocumentBuilderFactory;

import org.therandoms.textgen.TextGeneratorDictionary;
import org.therandoms.textgen.TextGeneratorDictionaryXMLWorker;
import org.w3c.dom.Document;

import android.os.AsyncTask;

/**
	 * An {@link AsyncTask} for loading from a grammar file.
	 * 
	 * @author Logan Widick
	 */
	public class GrammarFileLoadTask extends
			AsyncTask<String, Long, TextGeneratorDictionary<String>> {

		protected static final String TAG = "GrammarFileChooserStep";

		@Override
		protected TextGeneratorDictionary<String> doInBackground(
				String... params) {
			try {
				// make the document
				FileInputStream is = new FileInputStream(params[0]);
				Document doc = DocumentBuilderFactory.newInstance()
						.newDocumentBuilder().parse(is);

				// parse the document
				return TextGeneratorDictionaryXMLWorker
						.createFromXMLDocument(doc);
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
	}